<template>
  <div class="persondata">
    <div class="line">
      <span>|</span>
      <span>{{ cropName }}</span>
    </div>
    <div class="header">
      <div class="left">
        <el-date-picker
          v-model="value1"
          style="margin-right:1%"
          type="date"
          placeholder="开始时间"
        />
        <el-date-picker
          v-model="value1"
          style="margin-right:1%"
          type="date"
          placeholder="结束时间"
        />
        <el-input v-model="input" placeholder="请输入名称" style="width:200px;margin-right:1%" />
        <el-button size="medium">搜索</el-button>
      </div>
      <div class="right">
        <el-button type="primary" size="small" icon="el-icon-arrow-left" @click="goback">返回</el-button>
        <el-button v-show="role=='student'" type="primary" size="small" icon="el-icon-receiving" @click="exportData">导出调查数据</el-button>
        <el-button v-show="role=='student'" type="primary" size="small" icon="el-icon-document-copy" @click="trainningReport">实训报告</el-button>
        <el-button v-show="role=='student'" type="primary" size="small" icon="el-icon-document-add" @click="addData">新增</el-button>
      </div>
    </div>
    <div class="content">
      <el-table
        ref="multipleTable"
        v-loading="listLoading"
        stripe
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="77"
        />
        <el-table-column
          label="序号"
          width="180"
        >
          <template slot-scope="scope">{{ scope.$index+1 }}</template>
        </el-table-column>
        <el-table-column
          prop="studentName"
          label="姓名"
          width="180"
        />
        <el-table-column
          prop="className"
          label="班级"
          width="180"
        />
        <el-table-column
          prop="gradeName"
          label="年级"
          width="180"
          show-overflow-tooltip
        />
        <el-table-column
          prop="lastModifiedTime"
          label="提交时间"
          width="200"
          show-overflow-tooltip
        />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleLook(scope.$index, scope.row)"
            ><i class="el-icon-view" /></el-button>
            <el-button
              v-if="role=='student'"
              size="mini"
              type="text"
              style="color:green"
              @click="handleUpdate(scope.$index, scope.row)"
            ><i class="el-icon-edit" /></el-button>
            <el-button
              size="mini"
              type="text"
              style="color:red"
              @click="handleDelete(scope.$index, scope.row)"
            ><i class="el-icon-delete" /></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { getDataReportList, deleteReport, exportDataReportStudent } from '@/api/dataReport'
export default {
  name: 'Persondata',
  data() {
    return {
      role: '',
      cropId: '',
      cropName: '',
      studentId: '',
      input: '',
      value1: '',
      tableData: [],
      multipleSelection: [],
      listLoading: true
    }
  },
  created() {
    this.role = this.$store.getters.roles// 获取角色
    this.cropName = this.$store.getters.cropName
    console.log(this.$route.params)
    const { cropId, studentId } = this.$route.params
    this.cropId = cropId
    this.studentId = studentId == -1 ? this.$store.getters.id : studentId
    console.log(this.studentId)
    this.getDataReportList()
  },
  methods: {
    getDataReportList() {
      const data = {
        cropId: this.cropId,
        studentId: this.studentId
      }
      getDataReportList(data).then(res => {
        this.tableData = res.data.records.splice(0)
        this.listLoading = false
      })
    },
    goback() {
      this.$router.go(-1)
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleLook(index, row) { // 跳转到个人数据详情页面
      const data = {
        dataReportId: this.tableData[index].dataReportId,
        studentName: this.tableData[index].studentName
      }
      this.$router.push(`/trainning/persondatadetail/${this.tableData[index].dataReportId}/${this.tableData[index].studentName}`)
    },
    handleUpdate(index) {
      const data = {
        cropId: this.cropId,
        dataReportId: this.tableData[index].dataReportId,
        studentName: this.tableData[index].studentName
      }
      this.$router.push({
        path: '/trainning/updatedata',
        query: { data }
      })
    },
    handleDelete(index) {
      this.$confirm('是否确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        const data = {
          dataReportId: this.tableData[index].dataReportId
        }
        deleteReport(data).then(res => {
          this.getDataReportList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    trainningReport() { // 跳转到查看个人实训报告页面
      this.$router.push(`/trainning/studentReport/${this.cropId}`)
    },
    addData() { // 跳转到新增数据页面
      this.$router.push(`/trainning/adddata/${this.cropId}`)
    },
    exportData() { // 导出的是excel
      const query = {
        cropId: this.cropId,
        studentId: this.studentId
      }
      exportDataReportStudent(query).then(res => {
        const blob = new Blob([res.data])
        const downloadElement = document.createElement('a')
        const href = window.URL.createObjectURL(blob) // 创建下载的链接
        downloadElement.href = href
        downloadElement.download = '总数据.xls' // 下载后文件名
        document.body.appendChild(downloadElement)
        downloadElement.click() // 点击下载
        document.body.removeChild(downloadElement) // 下载完成移除元素
        window.URL.revokeObjectURL(href) // 释放掉blob对象
      })
    }
  }
}
</script>
<style scoped>
    .line{
        width: 100%;
        color: #345E8F;
        font-size: 16px;
        margin-bottom: 1%;
    }
    span:first-child{
        font-weight: bold;
        margin-right: 1%;
        font-size: 18px;
    }
    .header{
        display: flex;
        justify-content: space-between;
        margin-bottom: 1%;
    }
    .left{
        display: flex;
        justify-content: flex-start;
    }
</style>
